<?php
	class User
	{
		var $UserInfo = 		array (
			'fullname'	=> "",
			'email'   	=> "",
			'div_codes'   	=> "",
			'error'  		=> "");
			
		function user()
		{
		}

		function createUser($userFields) {
			global $DB;
			
			$temp_pass = str_shuffle($userFields['lastname'] . $userFields['firstname']);
			
			$db_string = $DB->compile_db_insert_string( 
				array (
					'FirstName'	=> $userFields['firstname'],
					'LastName' => $userFields['lastname'],
					'Username' =>  $userFields['username'],
					'Email' => $userFields['email'],
					'PasswordTemp' => $temp_pass
					)
				);
			
			$DB->query("INSERT INTO users (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");

			return $temp_pass;
		}
		
		function createGeneratedUser($userFields) {
			$userFields['firstname'] = substr($userFields['fullname'],0,strpos($userFields['fullname']," "));
			$userFields['lastname'] =  substr($userFields['fullname'],strpos($userFields['fullname']," ")+1);
			$userFields['username'] = $this->getGeneratedUsername();
			
			$this->createUser($userFields);

			return $userFields['username'];
		}
		
		function getGeneratedUsername() {
			global $DB;
			
			$DB->query("select max(convert(right(username,char_length(username)-4),unsigned))+1 as maxnum from lwbc_main.users where username like 'AUTO%'");
			if($row = $DB->fetch_row()) {
				if($row['maxnum'] <> '') {
					return 'AUTO'.$row['maxnum'];
				}
				else {
					return 'AUTO1';
				}
			}
			else {
				return 'AUTO1';
			}
		}
		
		function getTemporaryPassword($username) {
			global $DB;
						
			$DB->query("select PasswordTemp from users where username='" . $username . "'");
			$row = $DB->fetch_row();
			
			return $row['PasswordTemp'];
		}
		
		function GetUserInfo($username)
		{
			$this->UserInfo['error'] = "";
			
			global $DB;
			//check to see if the user exists
			$DB->query("SELECT * FROM users WHERE username='" . $username . "'");
			//if there is no user store the User Info and leave
			if($DB->get_num_rows() <= 0)
			{
				$this->ErrorAndLeave("userne");
			}
			//get the user's info
			$row = $DB->fetch_row();

			/*************** LOOK UP EMAIL ******************/
			$this->UserInfo['email'] = $row['Email'];
			
			/*************** LOOK UP FULL NAME ******************/
			$this->UserInfo['fullname'] = $this->GetFullName($username); 

			/*************** LOOK UP DIVISIONS ******************/
			$this->UserInfo['div_codes'] = $this->GetDivCodes($username);
			
			//if there were any errors clear the values
			if($this->UserInfo['error'] != "")
			{
				$this->UserInfo['fullname'] = "";
				$this->UserInfo['email'] = "";
				$this->UserInfo['div_codes'] = "";
			}

			return $this->UserInfo;
		}
	
		function GetFullName($username)
		{
			if($username != "")
			{
				//load the global database $DB
				global $DB;
				//find the fullname 
				$DB->query("SELECT FirstName, CONCAT(LEFT(MiddleName, 1), '.') AS MiddleInit, LastName FROM users WHERE username='" . $username . "'");
				//grab the row from the table
				$row = $DB->fetch_row();
				//build the FullName
				if($row['FirstName'] != "")
				{
					$FullName = $row['FirstName'];
				}
				if($row['MiddleInit'] != ".")
				{
					$FullName = $FullName . " " . $row['MiddleInit'];
				}
				if($row['LastName'] != "")
				{
					$FullName = $FullName . " " . $row['LastName'];
				}
			}
			else
			{
				$FullName = "";
			}
	
			return $FullName; 				
		}
		
		function GetDivCodes($username)
		{
			global $DB,$GlobalSettings;
			//find all the divcodes for a particular user
			$DB->query("SELECT * FROM ministry_members WHERE username = '" . $username . "'");
			$div_codes = "";
			$count = 1;
			while($row = $DB->fetch_row())
			{
				if($count == 1)
				{
					$div_codes = $row['MinistryID'];
				}
				else
				{
					if(trim($row['MinistryID']) != "")
					{
						$div_codes = $div_codes . "," . $row['MinistryID'];
					}
				}
				$count++;
			}
			return $div_codes;
		}
		
		function ErrorAndLeave($error)
		{
			$this->UserInfo['error'] = $error;
			return $this->UserInfo;
		}
	}	
?>
